package com.ayu.shortlink.distribute.mq.producer;

import cn.hutool.core.util.StrUtil;
import com.ayu.shortlink.distribute.common.constant.RocketMQConstant;
import com.ayu.shortlink.distribute.mq.basics.base.BaseSendMessageExtendDTO;
import com.ayu.shortlink.distribute.mq.basics.base.MessageWrapper;
import com.ayu.shortlink.distribute.mq.basics.event.CouponTemplateExecuteEvent;
import org.apache.rocketmq.common.message.MessageConst;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Component;

import java.util.UUID;

/**
 * 执行用户优惠券分发和刷盘的生产者
 */
@Component
public class CouponTaskActuallyDistributeProducer extends AbstractCommonSendProduceTemplate<CouponTemplateExecuteEvent>{

    public CouponTaskActuallyDistributeProducer(@Autowired RocketMQTemplate rocketMQTemplate) {
        super(rocketMQTemplate);
    }

    @Override
    protected BaseSendMessageExtendDTO buildBaseSendMessageExtendDTO(CouponTemplateExecuteEvent messageSendEvent) {
        return BaseSendMessageExtendDTO.builder()
                .eventName("真实执行刷盘")
                .topic(RocketMQConstant.TOPIC_ACTUALLY_EXECUTOR_TASK)
                .keys(messageSendEvent.getCouponTaskId())
                .sentTimeout(2000L)
                .build();
    }

    @Override
    protected Message<?> buildMessage(CouponTemplateExecuteEvent messageSendEvent, BaseSendMessageExtendDTO requestParam) {
        //设定唯一标识
        String keys = StrUtil.isEmpty(requestParam.getKeys()) ? UUID.randomUUID().toString() : requestParam.getKeys();
        return MessageBuilder.withPayload(new MessageWrapper(requestParam.getKeys(), messageSendEvent))
                .setHeader(MessageConst.PROPERTY_KEYS,keys)
                .setHeader(MessageConst.PROPERTY_TAGS, requestParam.getTag())
                .build();
    }
}
